<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="left">
            <el-form-item label="表名称" prop="tableName">
                <el-input v-model="form.tableName" clearable>
                    <template #prepend>{{ this.tablePrefix }}</template>
                </el-input>
            </el-form-item>
            <el-form-item label="表注释" prop="tableComment">
                <el-input v-model="form.tableComment" clearable></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable type="textarea" maxlength="255" show-word-limit/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        const checkExist = (rule, value, callback) => {
            if (!value || value === '') {
                callback() //条件不满足 也需要回调、返回，此次回调成功，因为外面有其他规则校验
                return;
            }
            this.exists(rule.field, value).then(res => {
                if (res.code !== 200) {
                    callback(new Error(res.msg))
                } else if (res.data) {
                    callback(new Error('表名称已存在')) // 返回校验失败
                } else {
                    callback()
                }
            })
        }
        return {
            mode: "add",
            titleMap: {
                add: '新增数据表',
                edit: '编辑数据表',
                show: '查看数据表'
            },
            visible: false,
            isSaveing: false,
            tablePrefix: '',
            //表单数据
            form: {
                id: "",
                projectId: null,
                dbId: null,
                moduleId: null,
                tableName: "",
                tableComment: "",
                remark: ""
            },
            //验证规则
            rules: {
                tableName: [
                    {required: true, message: '请输入表名称'}, {validator: checkExist, trigger: 'blur'}
                ],
                tableComment: [
                    {required: true, message: '请输入表注释'}
                ]
            }
        }
    },
    mounted() {

    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    const res = this.mode === 'add' ? await this.$API.design.table.post(this.form) : await this.$API.design.table.put(this.form);
                    this.isSaveing = false;
                    if (res.code === 200) {
                        this.$emit('success', this.form, this.mode)
                        this.visible = false;
                        this.$message.success("操作成功")
                    } else {
                        await this.$alert(res.msg, "提示", {type: 'error'})
                    }
                }
            })
        },
        // 验证字段是否存在
        async exists(name, value) {
            const fieldNames = ['moduleId', name]
            const fieldValues = [this.form.moduleId, value]
            return await this.$API.design.table.exists(fieldNames.join(), fieldValues.join(), this.form.id);
        },
        //表单注入数据
        setData(data) {
            this.tablePrefix = data.tablePrefix
            this.form.id = data.id ? data.id : ''
            this.form.projectId = data.projectId
            this.form.dbId = data.dbId
            this.form.moduleId = data.moduleId
            this.form.tableName = data.tableName
            this.form.tableComment = data.tableComment
            this.form.remark = data.remark

            //可以和上面一样单个注入，也可以像下面一样直接合并进去
            //Object.assign(this.form, data)
        }
    }
}
</script>

<style>
</style>
